﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;


namespace LeSoft.Application.Web.Areas.BaseManage.Controllers
{
    public class PropertyController : MvcControllerBase
    {
        //楼盘项目
        // GET: /BaseManage/Property/
        private OrganizeCache organizeCache = new OrganizeCache();
        private PStructCache pstructCache = new PStructCache();
        private AreaBLL areaBLL = new AreaBLL();

        #region 视图
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit)]
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取不同类型的树
        /// </summary>
        /// <param name="EnCode">树类型</param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "获取不同类型的树", "GetTreeByEnCode")]
        public ActionResult GetPropertyQueryTreeJson(string queryJson)
        {
            var treeList = new List<TreeEntity>();
            if (!string.IsNullOrEmpty(queryJson))
            {
                var queryParam = queryJson.ToJObject();
                string Type = !queryParam["Type"].IsEmpty() ? queryParam["Type"].ToString() : "1";
                string AreaId = !queryParam["AreaId"].IsEmpty() ? queryParam["AreaId"].ToString() : "310100";
                string Id = !queryParam["Id"].IsEmpty() ? queryParam["Id"].ToString() : "";
                bool IsSubstep = !queryParam["IsSubstep"].IsEmpty() ? queryParam["IsSubstep"].ToBool() : false;
                switch (Type)
                {
                    #region 机构树
                    case "1":
                        var data = organizeCache.GetList().ToList();
                        foreach (OrganizeEntity item in data)
                        {
                            TreeEntity tree = new TreeEntity();
                            bool hasChildren = data.Count(t => t.ParentId == item.OrganizeId) == 0 ? false : true;
                            tree.id = item.OrganizeId;
                            tree.text = item.FullName;
                            tree.value = item.OrganizeId;
                            tree.isexpand = true;
                            tree.complete = true;
                            tree.hasChildren = hasChildren;
                            tree.img = "fa fa-sitemap";
                            if (!OperatorProvider.Provider.Current().IsSystem) tree.parentId = item.OrganizeId == OperatorProvider.Provider.Current().CompanyId ? "0" : item.ParentId;
                            else tree.parentId = item.ParentId;
                            treeList.Add(tree);
                        }
                        break;
                    #endregion
                    #region 区域树
                    case "2":
                        var data1 = areaBLL.GetList(AreaId).ToList();
                        foreach (AreaEntity item in data1)
                        {
                            TreeEntity tree = new TreeEntity();
                            tree.id = item.AreaId;
                            tree.text = item.AreaName;
                            tree.value = item.AreaId;
                            tree.title = item.AreaCode;
                            tree.Attribute = "LAT";
                            tree.AttributeValue = item.LAT == null ? "" : item.LAT.ToString();
                            tree.parentId = "0";
                            tree.AttributeA = "LON";
                            tree.AttributeValueA = item.LON == null ? "" : item.LON.ToString();
                            tree.isexpand = false;
                            tree.complete = true;
                            tree.hasChildren = false;
                            treeList.Add(tree);
                        }
                        break;
                    #endregion
                    default:
                        break;
                }
            }
            return Content(treeList.TreeToJson());
        }
        #endregion

        #region 查询数据

        #endregion
       

    }
}
